#include "Parameters.h"

#include <sstream>

Parameter::Parameter(ParameterManager::PARAM_TYPE t, float v)
{
	type = t;
	sValue = "";
	fValue = v;
}

Parameter::Parameter(ParameterManager::PARAM_TYPE t, std::string v)
{
	type = t;
	fValue = 0;
	sValue = v;
}

ParameterManager::ParameterManager()
{
	vParams.clear();
}

ParameterManager::~ParameterManager()
{
	for (unsigned int i = 0; i < vParams.size(); ++i)
		delete (vParams[i]);
}

//TODO Verificar que los parametros existan. Por ej. -o PARAM
bool ParameterManager::load(int argc, char* argv[])
{
	Parameter* pParam;

	float tmpVal;
	std::string tmpParam;

	int i = 1;

	while (i < argc)
	{
		pParam = NULL;
		tmpParam.assign(argv[i]);

		if ((tmpParam == "-a") || (tmpParam == "--auto"))
			pParam = new Parameter(AUTO_EQUALIZE, 0);

		if ((tmpParam == "-B") || (tmpParam == "--blur"))
		{
			pParam = new Parameter(BLUR, 0);
		}

		if ((tmpParam == "-b") || (tmpParam == "--brightness"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(BRIGHTNESS, tmpVal);
		}

		if ((tmpParam == "-C") || (tmpParam == "--channel"))
		{
			// RED
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream R(tmpParam);
			R >> tmpVal;
			pParam = new Parameter(CHANNEL_R, tmpVal);
			vParams.push_back(pParam);

			// GREEN
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream G(tmpParam);
			G >> tmpVal;
			pParam = new Parameter(CHANNEL_G, tmpVal);
			vParams.push_back(pParam);

			// BLUE
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream B(tmpParam);
			B >> tmpVal;
			pParam = new Parameter(CHANNEL_B, tmpVal);
			vParams.push_back(pParam);

			pParam = new Parameter(CHANNEL, 0);
		}

		if ((tmpParam == "-c") || (tmpParam == "--contrast"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(CONTRAST, tmpVal);
		}

		if ((tmpParam == "-f") || (tmpParam == "--flip"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(FLIP, tmpVal);
		}

		if ((tmpParam == "-g") || (tmpParam == "--gamma"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(GAMMA, tmpVal);
		}

		if ((tmpParam == "-G") || (tmpParam == "--gaussian"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(GAUSSIAN, tmpVal);
		}

		if ((tmpParam == "-h") || (tmpParam == "--help"))
			return false;

		if ((tmpParam == "-i") || (tmpParam == "--input"))
		{
			i++;
			pParam = new Parameter(INPUT, std::string(argv[i]));
		}

		if ((tmpParam == "-I") || (tmpParam == "--invert"))
			pParam = new Parameter(INVERT, 0);

		if ((tmpParam == "-L") || (tmpParam == "--laplace"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(LAPLACE, tmpVal);
		}

		if ((tmpParam == "-o") || (tmpParam == "--output"))
		{
			i++;
			pParam = new Parameter(OUTPUT, std::string(argv[i]));
		}

		if ((tmpParam == "-S") || (tmpParam == "--sharpen"))
		{
			pParam = new Parameter(SHARPEN, 0);
		}

		if ((tmpParam == "-T") || (tmpParam == "--threshold"))
		{
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream o(tmpParam);
			o >> tmpVal;
			pParam = new Parameter(THRESHOLD, tmpVal);
		}

		if ((tmpParam == "-m") || (tmpParam == "--grayscale"))
		{
			pParam = new Parameter(GRAYSCALE, 0);
		}

		if ((tmpParam == "-s") || (tmpParam == "--silhouette"))
		{
			// Agregar a la Base o no
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream R(tmpParam);
			R >> tmpVal;
			pParam = new Parameter(ADD_DB, tmpVal);
			vParams.push_back(pParam);

			// Radio de tolerancia
			i++;
			tmpParam.assign(argv[i]);
			std::istringstream G(tmpParam);
			G >> tmpVal;
			pParam = new Parameter(PIVOTE, tmpVal);
			vParams.push_back(pParam);

			pParam = new Parameter(SILHOUETTE, 0);
		}

		if (pParam)
			vParams.push_back(pParam);

		i++;
	}

	return true;
}

Parameter* const ParameterManager::getParameter(PARAM_TYPE type)
{
	for (unsigned int i = 0; i < vParams.size(); ++i)
		if (vParams[i]->getType() == type)
			return vParams[i];

	return NULL;
}



